/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evolve.webbot.util;

import  java.util.Calendar;
import  java.util.ArrayList;
import  java.util.StringTokenizer;
import  java.text.SimpleDateFormat;
import  org.apache.log4j.Logger;
import  com.evolve.webbot.bean.LinkRetail;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
 *
 * @author DarthAE
 */
public class Utileria 
{
    static Logger log = Logger.getLogger(Utileria.class);
    private static SimpleDateFormat sdf01 = new SimpleDateFormat("dd/MM/yyyy");
    private static SimpleDateFormat sdf02 = new SimpleDateFormat("dd-MM-yyyy");
    private static SimpleDateFormat sdf03 = new SimpleDateFormat("dd.MM.yyyy");
    
    public Utileria(){}
    
    public String cleanValueToken(String value)
    {
        String cleanValue = null;
        
        StringTokenizer tokens = new StringTokenizer(value);

        while(tokens.hasMoreTokens())
        {
           cleanValue = tokens.nextToken().trim();
           System.out.println(cleanValue);
        }
        
        return cleanValue;
    }
    
    public String cleanValue(String value)
    {
        String cleanValue = null;
        
        if(value!=null)
        {
             for(int i=0;i<255;i++)
             {   
               if(i!=46 && (i<=47 || i>=58))
               {
                 char n = (char)i;
                 value = value.replace(n,' ');
                 value = value.replaceAll(" ","");
               }                 
             }             
             cleanValue = value;       
        }
        
        return cleanValue;
    }
    
    public int getActualMonth()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        int actualMonth      = 0;
        
        Calendar calendar = Calendar.getInstance();
        
        try{
             actualMonth = Integer.parseInt(sdf.format(calendar.getTime()));    
           }catch(Exception e){System.out.println(e);}
        
        return actualMonth;
    }
    
    public String getLastDayOfMonth(int monthNum)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        String lastDayMonth  = null;
        int    lastDate      = 0;
        
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH,monthNum);
        
        lastDate = calendar.getActualMaximum(calendar.DATE);
        
        calendar.set(calendar.DATE,lastDate);
        
        lastDayMonth = sdf.format(calendar.getTime());
        
        return lastDayMonth;
    }
    
    public String getFirstDayOfMonth(int monthNum)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");        
        String firstDayMonth = null;
        int    firstDate     = 0;
        
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH,monthNum);
        
        firstDate = calendar.getActualMinimum(calendar.DATE);
        
        calendar.set(calendar.DATE,firstDate);
        
        firstDayMonth = sdf.format(calendar.getTime());    
        
        return firstDayMonth;
    }
    
    public String getMiddleDayOfMonth(String actualDate)
    {    
        String middleDayMonth=null;
        
        middleDayMonth = "15"+actualDate.substring(2);
        
        return middleDayMonth;
    }    
    public String dateFormat(int opt,Calendar calendar)
    {
        String formatedDate = null;
        
        switch(opt)
        {
            case 1: formatedDate  = sdf01.format(calendar);
                    break;
            case 2: formatedDate  = sdf02.format(calendar);
                    break;              
            case 3: formatedDate  = sdf03.format(calendar);
                    break;
            default: formatedDate = sdf01.format(calendar);
                     break;
        }
        
        return formatedDate;
    }
    
    public ArrayList<String> getRetailFileLinks(String content)
    {
        String value ="";
        String link  ="";
        ArrayList<String> elementList = new ArrayList<String>();
        ArrayList<String> linkList = new ArrayList<String>();
        
        StringTokenizer tokens = new StringTokenizer(content,";");
                           
        while(tokens.hasMoreTokens())
        {
           value = tokens.nextToken().trim();
           if(value.contains("FileNameArray") ||
              value.contains("FileExtArray"))
           {
              value=value.substring(value.indexOf("\""));
              value=value.replace("\"","");
              elementList.add(value);
           }
       }
                           
       for(int i=0;i<elementList.size();i++)
       {
           if(3<elementList.get(i).length())
           {
               link=elementList.get(i)+"."+elementList.get(i+1);
               
               if(link.contains(".txt"))
               {
                   linkList.add(link);
               }
           }           
       }
       
       return linkList;
    }
    
    public ArrayList<String> getRetailFileLinksDates(String content)
    {
        String value ="";
        String link  ="";
        ArrayList<String> elementList = new ArrayList<String>();
        ArrayList<String> linkList = new ArrayList<String>();
        
        StringTokenizer tokens = new StringTokenizer(content,";");
                           
        while(tokens.hasMoreTokens())
        {
           value = tokens.nextToken().trim();
           if(value.contains("evolve_webbot_"))
           {
              
              value=value.substring(value.length()-11,value.length()-1);
              value=value.trim();
              value=value.substring(0,8);
              log.info(value);
              elementList.add(value);
           }
       }       
       
       return elementList;
    }    
    
    public ArrayList<String> getRetailFileLinks(String content, int opt)
    {
        String value ="";
        String link  ="";
        String fecha ="";
        int    count =0;
        ArrayList<String> elementList = new ArrayList<String>();
        ArrayList<String> linkList = new ArrayList<String>();
        
        StringTokenizer tokens = new StringTokenizer(content,";");
                           
        while(tokens.hasMoreTokens())
        {
           value = tokens.nextToken().trim();
           if(value.contains("ReqNameArray") ||
              value.contains("FileNameArray")||
              value.contains("FileExtArray"))
           {
              value=value.substring(value.indexOf("\""));
              value=value.replace("\"","");
              elementList.add(value);
           }
         }
                           
       for(int i=0;i<elementList.size();i++)
       {
           if( !elementList.contains("/Outbound0") &&
               3<elementList.get(i).length())
           {
               if(i<elementList.size() - 2)
               {
                                  
                 link  = elementList.get(i)+"|"+elementList.get(i+1)+"."+elementList.get(i+2);
               
                 if(link.contains(".txt"))
                 {
                   if(elementList.get(i).contains("Report"))
                   {
                       linkList.add(link);
                   }                   
                 }   
               }
           }           
       }
       
       return linkList;
    }    
    
     public ArrayList<String> getValues(String linea)
     {
        ArrayList<String> listaDatos = new ArrayList<String>();
        StringTokenizer tokens = null;
        String  value     = null;
        
        tokens = new StringTokenizer(linea,"\t");
        
        while(tokens.hasMoreTokens())
        {
            value=tokens.nextToken().trim();
            /*log.info(value);*/
            listaDatos.add(value);
        }
        
        return listaDatos;
     }
     
     public int getNumMonthFromStrDate(String strFec)
     {
         String strMonth="";
         int numMonth = 0;
         
         if(strFec.contains("/")&&
                 strFec.length()==10)
         {
             numMonth=Integer.parseInt(strFec.substring(3,5));
         }
         
         return numMonth;
     }
     
     public String getNumMonthFromNumber(int numMes)
     {
         String mes="";
         
         switch(numMes)
         {
             case 12: mes="enero";
                     break;
             case 1: mes="febrero";
                     break;
             case 2: mes="marzo";
                     break;
             case 3: mes="abril";
                     break;
             case 4: mes="mayo";
                     break;
             case 5: mes="junio";
                     break;
             case 6: mes="julio";
                     break;
             case 7: mes="agosto";
                     break;
             case 8: mes="septiembre";
                     break;
             case 9: mes="octubre";
                     break;
             case 10: mes="noviembre";
                     break;
             case 11: mes="diciembre";
                     break;
             default: log.info("opcion no contemplada "+String.valueOf(numMes));
                     break;
         }
         
         return mes;
     }
     
    public String getProperty(String propertyFile, String propertyKey)
    {
       Properties properties = new Properties();
       String property="";
       
       try{  
             String propertyPath= propertyFile;
             
             log.info("la localización del archivo de properties es: "+propertyPath);
             
             InputStream fis = new FileInputStream(propertyPath);
             properties.load(fis);
             
             property = properties.getProperty(propertyKey);
           
          }catch(IOException e){
                                 log.error("error al recuperar archivo de configuración para la aplicación",e);}  
       return property;
    }     
}
